//
//  MeViewController.m
//  Socrates
//
//  Created by shy on 15/4/6.
//  Copyright (c) 2015年 shy. All rights reserved.
//

#import "MeViewController.h"

#import "SettingsViewController.h"
#import "UserCell.h"
#import "MyActivityViewController.h"
#import "MyCollectionViewController.h"
#import "MyWealthViewController.h"

typedef enum {
    kMe_user_information_item = 0,
    kMe_user_age_item,
    kMe_user_school_item,
    kLimitation_Num,
} MeItems;

static const int kUserRowHeight = 85;
static const int kAgeSchoolRowHeight = 40;
@interface MeViewController () {
    UIButton *myActivity_;
    UIButton *myCollection_;
    UIButton *myWealth_;
}

@property (strong, nonatomic) UIBarButtonItem *settinsBarBtn;

@end

@implementation MeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor colorWithRed:239/255. green:239/255. blue:244/255. alpha:1.];
    [self.tableView registerNib:[UINib nibWithNibName:@"UserCell" bundle:nil] forCellReuseIdentifier:@"UserCellIdentifier"];
    self.tableView.separatorInset = UIEdgeInsetsMake(0, 23, 0, 15);
    self.navigationItem.title = @"我的";
    self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName: [UIColor whiteColor]};
    self.tableView.scrollEnabled = NO;
    self.navigationItem.rightBarButtonItem = [self makeBarBtn];
    [self configMyView];
}

- (void)configMyView {
    CGFloat width = [UIScreen mainScreen].bounds.size.width;
    CGFloat imageWidth = 125;
    CGFloat imageHeight = 95;
    CGFloat viewHeight = 85;
    
    UIView *topLine = [[UIView alloc] initWithFrame:CGRectMake(0, 0, width, 1)];
    topLine.backgroundColor = [UIColor colorWithRed:224/255. green:224/255. blue:224/255. alpha:1.];
    [self.myView addSubview:topLine];
    
    myActivity_ = [[UIButton alloc] initWithFrame:CGRectMake((width/3 - imageWidth)/2, (viewHeight - imageHeight)/2, imageWidth, imageHeight)];
    [myActivity_ setImage:[UIImage imageNamed:@"my_activity"] forState:UIControlStateNormal];
    [myActivity_ addTarget:self action:@selector(clickMyActivity:) forControlEvents:UIControlEventTouchUpInside];
    [self.myView addSubview:myActivity_];
    
    UIView *separateLineA = [[UIView alloc] initWithFrame:CGRectMake(width/3, 15, 1, viewHeight - 30)];
    separateLineA.backgroundColor = [UIColor colorWithRed:224/255. green:224/255. blue:224/255. alpha:1.];
    [self.myView addSubview:separateLineA];
    
    myCollection_ = [[UIButton alloc] initWithFrame:CGRectMake((width - imageWidth)/2, (viewHeight - imageHeight)/2, imageWidth, imageHeight)];
    [myCollection_ setImage:[UIImage imageNamed:@"my_collection"] forState:UIControlStateNormal];
    [myCollection_ addTarget:self action:@selector(clickMyCollection:) forControlEvents:UIControlEventTouchUpInside];
    [self.myView addSubview:myCollection_];
    
    UIView *separateLineB = [[UIView alloc] initWithFrame:CGRectMake((width*2)/3, 15, 1, viewHeight - 30)];
    separateLineB.backgroundColor = [UIColor colorWithRed:224/255. green:224/255. blue:224/255. alpha:1.];
    [self.myView addSubview:separateLineB];
    
    myWealth_ = [[UIButton alloc] initWithFrame:CGRectMake((width*5)/6 - imageWidth/2, (viewHeight - imageHeight)/2, imageWidth, imageHeight)];
    [myWealth_ setImage:[UIImage imageNamed:@"my_wealth"] forState:UIControlStateNormal];
    [myWealth_ addTarget:self action:@selector(clickMyWealth:) forControlEvents:UIControlEventTouchUpInside];
    [self.myView addSubview:myWealth_];
    
    UIView *downLine = [[UIView alloc] initWithFrame:CGRectMake(0, viewHeight, width, 1)];
    downLine.backgroundColor = [UIColor colorWithRed:224/255. green:224/255. blue:224/255. alpha:1.];
    [self.myView addSubview:downLine];
    
}

- (UIBarButtonItem *)makeBarBtn {
    UIButton *settings = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 22, 20)];
    [settings setBackgroundImage:[UIImage imageNamed:@"me_settings"] forState:UIControlStateNormal];
    [settings addTarget:self action:@selector(clickSettings:) forControlEvents:UIControlEventTouchUpInside];
    self.settinsBarBtn = [[UIBarButtonItem alloc] initWithCustomView:settings];
    
    return self.settinsBarBtn;
}

- (void)clickSettings: (id)sender {
    SettingsViewController *vc = [[SettingsViewController alloc] initWithNibName:@"SettingsViewController" bundle:nil];
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)clickMyActivity: (id)sender {
    MyActivityViewController *vc = [[MyActivityViewController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)clickMyCollection: (id)sender {
    MyCollectionViewController *vc = [[MyCollectionViewController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)clickMyWealth: (id)sender {
    MyWealthViewController *vc = [[MyWealthViewController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 22;
}

- (UIView*)tableView:(UITableView*)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 20)];
    view.backgroundColor = [UIColor colorWithRed:239/255. green:239/255. blue:244/255. alpha:1.];
    return view;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    switch ([indexPath row]) {
        case kMe_user_information_item:
            return kUserRowHeight;
            break;
        case kMe_user_age_item:
            return kAgeSchoolRowHeight;
        case kMe_user_school_item:
            return kAgeSchoolRowHeight;
        default:
            break;
    }
    return 0;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return kLimitation_Num;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *cellIdentifier = @"UserCellIdentifier";
    UILabel *detailAge = [[UILabel alloc] initWithFrame:CGRectMake(105, 0, [UIScreen mainScreen].bounds.size.width - 145, 40)];
    UITableViewCell *cell;
    switch ([indexPath row]) {
        case kMe_user_information_item:
            cell = [self.tableView dequeueReusableCellWithIdentifier:cellIdentifier];
            if (!cell) {
                cell = [[UserCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
            }
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            break;
        case kMe_user_age_item:
            cell = [[UITableViewCell alloc] init];
            cell.textLabel.text = @"年龄";
            cell.textLabel.font = [UIFont systemFontOfSize:16.];
            cell.textLabel.textColor = [UIColor blackColor];
            detailAge.text = @"21岁";
            detailAge.font = [UIFont systemFontOfSize:16.];
            detailAge.textColor = [UIColor grayColor];
            [cell addSubview:detailAge];
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            break;
        case kMe_user_school_item:
            cell = [[UITableViewCell alloc] init];
            cell.textLabel.text = @"学校";
            cell.textLabel.font = [UIFont systemFontOfSize:16.];
            cell.textLabel.textColor = [UIColor blackColor];
            detailAge.text = @"对外经济贸易大学";
            detailAge.font = [UIFont systemFontOfSize:16.];
            detailAge.textColor = [UIColor grayColor];
            [cell addSubview:detailAge];
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            break;
        default:
            break;
    }
    return cell;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
